#include <assert.h>
#include <QDebug>
#include "mallet_drawer.h"

void MalletDrawer::init() {
    assert(m_vbo);
    assert(m_vao);
    // 顶点数据搬运至显存
    m_vbo->allocate(m_mallet->getVertexData()
            , m_mallet->getVertexPointSize() * 3 * sizeof(float));
    // 设置属性
    m_program->setAttribute(m_program->getAttrPosition(), 3, 3 * sizeof(float), 0);
    // enable属性
    m_program->enableAttribute(m_program->getAttrPosition());
}

void MalletDrawer::draw(QMatrix4x4 &modelViewProjectM) {
    // 绘制图形
    m_vao->bind();
    m_program->bind();
    // 设置变换矩阵
    glUniformMatrix4fv(m_program->getUniformProjMatrix()
            , 1
            , false
            , modelViewProjectM.data());
    int uColorLoc = static_cast<SimpleShaderProgram *>(m_program)->getUniformColor();
    // 底部圆筒
    QVector4D cylinderC = m_mallet->getCylinderColor();
    glUniform4f(uColorLoc, cylinderC.x(), cylinderC.y(), cylinderC.z(), cylinderC.w());
    glDrawArrays(GL_TRIANGLE_STRIP
            , m_mallet->getBaseCylinderPointStart()
            , m_mallet->getCylinderPointSize());
    // 上部圆盘
    QVector4D circleC = m_mallet->getCircleColor();
    glUniform4f(uColorLoc, circleC.x(), circleC.y(), circleC.z(), circleC.w());
    glDrawArrays(GL_TRIANGLE_FAN
            , m_mallet->getBaseCirclePointStart() 
            , m_mallet->getCirclePointSize());
    // 手柄圆筒
    glUniform4f(uColorLoc, cylinderC.x(), cylinderC.y(), cylinderC.z(), cylinderC.w());
    glDrawArrays(GL_TRIANGLE_STRIP
            , m_mallet->getHandleCylinderPointStart()
            , m_mallet->getCylinderPointSize());
    // 手柄圆盘
    glUniform4f(uColorLoc, circleC.x(), circleC.y(), circleC.z(), circleC.w());
    glDrawArrays(GL_TRIANGLE_FAN
            , m_mallet->getHandleCirclePointStart()
            , m_mallet->getCirclePointSize());
}
